package vns.client.presenter;

import java.util.ArrayList;
import java.util.List;

import vns.client.event.ObjectTypeEditEvent;
import vns.client.helper.RPCCall;
import vns.client.service.ObjectPropertyServiceAsync;
import vns.client.service.ObjectPropertyTypeService;
import vns.client.service.ObjectPropertyTypeServiceAsync;
import vns.client.view.ObjectTypeDetailUI;
import vns.shared.ObjectPropertyDTO;
import vns.shared.ObjectPropertyTypeDTO;
import vns.shared.ObjectTypeDTO;

import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class ObjectTypeDetailPresenter implements Presenter {

	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
		doInitTable();
	}

	public interface Display {
		Button getUpdateButton();

		ArrayList<ObjectPropertyDTO> getListRow();

		boolean setListRow(List<ObjectPropertyDTO> listRow);

		ArrayList<ObjectPropertyTypeDTO> getListPType();

		boolean setListPType(List<ObjectPropertyTypeDTO> listPType);

		ObjectTypeDTO getCurrentObjectTypeDTO();

		void setCurrentObjectTypeDTO(ObjectTypeDTO o);

		Widget asWidget();

		void render();
	}

	private final Display display;
	private final SimpleEventBus eventBus;
	private final ObjectPropertyServiceAsync rpcService;
	private final ObjectPropertyTypeServiceAsync rpcServicePtype = GWT
			.create(ObjectPropertyTypeService.class);

	public ObjectTypeDetailPresenter(ObjectTypeDetailUI view,
			SimpleEventBus eventBus, ObjectPropertyServiceAsync model,
			ObjectTypeDTO objectTypeDTO) {
		this.display = view;
		this.eventBus = eventBus;
		this.rpcService = model;
		display.setCurrentObjectTypeDTO(objectTypeDTO);
		// Window.alert("Otype = " + objectTypeDTO.getName() + "|" +
		// KeyFactory.keyToString(display.getCurrentObjectTypeDTO().getId()));
		bind();
	}

	private boolean bind() {
		display.getUpdateButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doUpdate();
			}
		});
		return false;
	}

	/***************************************************************************/

	private void doInitTable() {
		// Window.alert("doInitTable() presenter");
		new RPCCall<ArrayList<ObjectPropertyDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Không load được" + caught.toString());
			}

			@Override
			public void onSuccess(ArrayList<ObjectPropertyDTO> result) {
				display.getListRow().addAll(result);
				// Window.alert("NumRow Result = "
				// + Integer.toString(result.size()));
				// Window.alert("NumRow ListRow = "
				// + Integer.toString(display.getListRow().size()));

				new RPCCall<ArrayList<ObjectPropertyTypeDTO>>() {
					// Load kieu du lieu

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Không load được kiểu dữ liệu!"
								+ caught.toString());
					}

					@Override
					public void onSuccess(
							ArrayList<ObjectPropertyTypeDTO> result) {
						// Window.alert("Result ObjectPropertyTypeDTO " +
						// result.size());
						display.setListPType(result);
						display.render();
					}

					@Override
					protected void callService(
							AsyncCallback<ArrayList<ObjectPropertyTypeDTO>> cb2) {
						rpcServicePtype.Loadall(cb2);
					}

				}.retry(3);
			}

			@Override
			protected void callService(
					AsyncCallback<ArrayList<ObjectPropertyDTO>> cb) {
				rpcService.Loadall(display.getCurrentObjectTypeDTO(), cb);//day
			}
		}.retry(3);
		// Window.alert("End init table presenter");
	}

	private void doUpdate() {
//		Window.alert("Do Update, các phần từ được update là");
//		ArrayList<ObjectPropertyDTO> listNew = display.getListRow();
//		Window.alert("ListUpdate.Size = " + listNew.size());
//		for (ObjectPropertyDTO o : listNew) {
//			Window.alert(o.getOptype());
//		}
		new RPCCall<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Có lỗi khi đang cập nhật! \n "
						+ caught.toString());
			}

			@Override
			public void onSuccess(Void result) {
				Window.alert("Cập nhật thành công! \n ");
			}

			@Override
			protected void callService(AsyncCallback<Void> cb) {
				rpcService.Updateall(
						((List<ObjectPropertyDTO>) display.getListRow()),
						display.getCurrentObjectTypeDTO(), cb);
			}
		}.retry(3);
	}

}
